﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Utility.Messaging;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

namespace System.Utility.UI
{
    public class PageUtility
    {
        public static void RefreshByType(Control[] controlCollection)
        {
            foreach (Control ctrl in controlCollection)
            {
                if (ctrl is TextBox)
                {
                    TextBox t = (TextBox)ctrl;
                    t.Text = "";
                }
                else if(ctrl is HtmlInputText)
                {
                    HtmlInputText it = (HtmlInputText)ctrl;
                    it.Value = "";                    
                }
                else if (ctrl is DropDownList)
                {
                    DropDownList ddl = (DropDownList)ctrl;
                    ddl.SelectedIndex = -1;
                }                
            }
        }

        public static void Refresh(ControlCollection controlCollection)
        {
            foreach (Control ctrl in controlCollection)
            {
                if (ctrl is TextBox)
                {
                    TextBox t = (TextBox)ctrl;
                    t.Text = "";
                    break;
                }
                else if (ctrl is DropDownList)
                {
                    DropDownList ddl = (DropDownList)ctrl;
                    ddl.SelectedIndex = -1;
                    break;
                }
                else
                    Refresh(ctrl.Controls);
            }
        }

        public static void RegisterClientScriptBlockByMessageId(System.Web.UI.Page page, string messageCode)
        {
            string message = MessageFactory.GetMessageByCode(messageCode);

            page.ClientScript.RegisterClientScriptBlock(page.GetType(), "Javascript", "<script>alert('" + message + "')</script>");
        }

        public static void RegisterSuccessScriptBlockByMessageId(System.Web.UI.Page page)
        {
            string message = "Data updated successfully";

            page.ClientScript.RegisterClientScriptBlock(page.GetType(), "Javascript", "<script>alert('" + message + "')</script>");
        }


    }
}
